package com.ibigcat.module.mdm.service.impl;

import com.ibigcat.module.mdm.service.CommodityGroupService;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.ibigcat.module.mdm.controller.admin.commoditygroup.vo.*;
import com.ibigcat.module.mdm.dao.entity.CommodityGroupDO;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.util.object.BeanUtils;

import com.ibigcat.module.mdm.dao.mapper.CommodityGroupMapper;

import static com.ibigcat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.ibigcat.module.mdm.enums.ErrorCodeConstants.*;

/**
 * 组套商品管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CommodityGroupServiceImpl implements CommodityGroupService {

    @Resource
    private CommodityGroupMapper commodityGroupMapper;

    @Override
    public Long createCommodityGroup(CommodityGroupSaveReqVO createReqVO) {
        // 插入
        CommodityGroupDO commodityGroup = BeanUtils.toBean(createReqVO, CommodityGroupDO.class);
        commodityGroupMapper.insert(commodityGroup);
        // 返回
        return commodityGroup.getId();
    }

    @Override
    public void updateCommodityGroup(CommodityGroupSaveReqVO updateReqVO) {
        // 校验存在
        validateCommodityGroupExists(updateReqVO.getId());
        // 更新
        CommodityGroupDO updateObj = BeanUtils.toBean(updateReqVO, CommodityGroupDO.class);
        commodityGroupMapper.updateById(updateObj);
    }

    @Override
    public void deleteCommodityGroup(Long id) {
        // 校验存在
        validateCommodityGroupExists(id);
        // 删除
        commodityGroupMapper.deleteById(id);
    }

    private void validateCommodityGroupExists(Long id) {
        if (commodityGroupMapper.selectById(id) == null) {
            throw exception(COMMODITY_GROUP_NOT_EXISTS);
        }
    }

    @Override
    public CommodityGroupDO getCommodityGroup(Long id) {
        return commodityGroupMapper.selectById(id);
    }

    @Override
    public PageResult<CommodityGroupDO> getCommodityGroupPage(CommodityGroupPageReqVO pageReqVO) {
        return commodityGroupMapper.selectPage(pageReqVO);
    }

}